package daoImp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import twitter4j.Tweet;
import util.DateUtil;
import dao.TweetsDAO;

public class TweetsDAOImp implements TweetsDAO{

	private static TweetsDAOImp uniqueInstance = new TweetsDAOImp();
	private ConnectionPool pool = ConnectionPool.getInstance();
	
	public static TweetsDAO getInstance() {
		return uniqueInstance;
	}

	private TweetsDAOImp() {
	}

	public ArrayList<Long> getTweetsIDsByTrend(String trend, Date date) {
		String sql = null;
		ArrayList<Long> tweetIDs = new ArrayList<Long>();
		try {
			Connection connection;
			try {
				connection = pool.getConnection();
			} catch (Exception e) {
				e.printStackTrace();
				return tweetIDs;
			}
			Statement stmt = connection.createStatement();
			String dateStr = DateUtil.getDateStr(date);
			trend = trend.replaceAll("'", "\\\\'").replaceAll("\\)", "\\\\)").replaceAll("\\(", "\\\\(");
			sql = "select tweet_id from tweets where trend = '" + trend + "' and date = '" + dateStr +"';";
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				long tweetid = rs.getLong(1);
				tweetIDs.add(tweetid);
			}
			pool.returnConnection(connection);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return tweetIDs;
	}
}
